﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_KhuyenMaiVang.Models;
using Project_KhuyenMaiVang.DataProvider;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Project_KhuyenMaiVang.DAO
{
    public class HinhAnhDAO : MyDataProvider
    {
        // Thêm tài khoản vào database
        // return true nếu thành công, false nếu thất bại
        public bool insertImage(HinhAnhDTO inforInput)
        {
            bool isOK = true;
            string insertCommand ="";
            try
            {
                //string insertCommand = "INSERT INTO HinhAnh VALUES(" +
                insertCommand = @"INSERT INTO HinhAnh VALUES(N'" +                
                inforInput.TenHinhAnh + "', N'" +
                inforInput.MoTa + "',N'" +
                inforInput.Link + "'," +
                inforInput.MaSanPham + ")";
                
                //executeNonQuery(insertCommand);
            }
            catch(Exception e)
            {
                string ex = e.ToString();
                isOK = false;
            }
            executeNonQuery(insertCommand);
            return isOK;
        }

        // Thêm hình ảnh vào database
        // return MaHinhAnh nếu thành công, -1 nếu thất bại
        public int themHinhAnh(HinhAnhDTO infoInput)
        {
            int maHinhAnh = -1 ;
            string insertCommand = "";
            try
            {
                //string insertCommand = "INSERT INTO HinhAnh VALUES(" +
                insertCommand = @"INSERT INTO HinhAnh VALUES(N'" +
                infoInput.TenHinhAnh + "', N'" +
                infoInput.MoTa + "',N'" +
                infoInput.Link + "'," +
                infoInput.MaSanPham + ")";

                //executeNonQuery(insertCommand);
            }
            catch (Exception e)
            {
                string ex = e.ToString();
                maHinhAnh = -1;
            }
            executeNonQuery(insertCommand);
            maHinhAnh = getMaHinhAnh(infoInput);
            return maHinhAnh;
        }
        public int getMaHinhAnh(HinhAnhDTO hinhAnhDTO)
        {
            int result = -1;
            try
            {
                string QueryString = @"select * from HinhAnh where MaSanPham = " + hinhAnhDTO.MaSanPham
                                                                  + " and TenHinhAnh = N'" + hinhAnhDTO.TenHinhAnh
                                                                  + "' and MoTa = N'" + hinhAnhDTO.MoTa + "'";

                _Adapter = new SqlDataAdapter(QueryString, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);

                DataTable dt = dataset.Tables[0];
                object ob = GetDataFromDataRow(dt, 0);
                hinhAnhDTO = (HinhAnhDTO)ob;
                result = hinhAnhDTO.MaSanPham;
            }
            catch (System.Exception ex)
            {
                result = -1;
            }
            return result;
        }

        public List<HinhAnhDTO> GetListHinhAnhBy(int MaSanPham)
        {
            try
            {
                List<HinhAnhDTO> result = new List<HinhAnhDTO>();

                string query = "select * from HinhAnh where MaSanPham = '" + MaSanPham.ToString() + "'";
                _Adapter = new SqlDataAdapter(query, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    HinhAnhDTO ob = (HinhAnhDTO)GetDataFromDataRow(dt, i);
                    result.Add(ob);
                }

                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            HinhAnhDTO hinhAnhDTO = new HinhAnhDTO();

            hinhAnhDTO.MaHinhAnh = Int32.Parse(dt.Rows[i]["MaHinhAnh"].ToString());
            hinhAnhDTO.MaSanPham = Int32.Parse(dt.Rows[i]["MaSanPham"].ToString());
            hinhAnhDTO.TenHinhAnh = dt.Rows[i]["TenHinhAnh"].ToString();
            hinhAnhDTO.MoTa = dt.Rows[i]["MoTa"].ToString();
            hinhAnhDTO.Link = dt.Rows[i]["Link"].ToString();

            return (object)hinhAnhDTO;
        }
    }
}